package org.zjx.strategy;

import org.zjx.command.ICommand;
import org.zjx.core.GameWindow;

import java.awt.image.BufferedImage;
import java.util.*;

public class StrategyFactory {
    private final List<IGameStrategy> strategies = new ArrayList<>();
    private final Map<String, IGameStrategy> strategyMap = new HashMap<>();

    /**
     * 注册策略
     */
    public void registerStrategy(IGameStrategy strategy) {
        strategies.add(strategy);
        strategyMap.put(strategy.getStrategyName(), strategy);
        // 按优先级排序
        strategies.sort(Comparator.comparingInt(IGameStrategy::getPriority));
        System.out.println("[StrategyFactory] Registered strategy: " + strategy.getStrategyName());
    }

    /**
     * 根据截图选择合适的策略并生成指令
     */
    public ICommand selectAndExecuteStrategy(GameWindow window, BufferedImage screenshot) {
        if (screenshot == null) {
            return null;
        }

        // 按优先级顺序检查策略适用性
        for (IGameStrategy strategy : strategies) {
            if (strategy.isApplicable(window, screenshot)) {
                System.out.println("[StrategyFactory] Selected strategy: " + strategy.getStrategyName());
                return strategy.analyze(window, screenshot);
            }
        }

        // 默认策略
        System.out.println("[StrategyFactory] Using default strategy");
        return getDefaultStrategy().analyze(window, screenshot);
    }

    /**
     * 获取特定策略
     */
    public IGameStrategy getStrategy(String name) {
        return strategyMap.get(name);
    }

    /**
     * 获取默认策略
     */
    public IGameStrategy getDefaultStrategy() {
        return strategyMap.get("DefaultStrategy");
    }

    /**
     * 获取所有策略
     */
    public List<IGameStrategy> getAllStrategies() {
        return new ArrayList<>(strategies);
    }
}
